#!/usr/bin/python
# Humble Unrefined Game Engine Build script.
# meant to make cross platform compiling a snap on linux, Mac and other unix based Systems.
# requires Scons to be of use.
import os
import glob
import os
import platform
import imp

check_prereqs = imp.load_source('*', 'tools/hugeprepreqcheck.py')
build_lib_HumbleGL   = imp.load_source('*',\
                    'tools/huge_build_humblegl.py')
build_lib_HumbleCore   = imp.load_source('*',\
                    'tools/huge_build_humbleCore.py')					
build_lib_TinyXML    = imp.load_source('*', 'tools/huge_build_TinyXML.py')
build_lib_PAWN    = imp.load_source('*', 'tools/huge_build_libPAWN.py')
build_lib_SMALL    = imp.load_source('*', 'tools/huge_build_libSMALL.py')
build_bin_humbletest = imp.load_source('*', 'tools/huge_test_binary.py')
if None in (
    check_prereqs,
    # dependencies.
    build_lib_TinyXML,
	build_lib_SMALL,
	build_lib_PAWN,
    # Humble Sources	
	build_lib_HumbleCore,
    build_lib_HumbleGL,
    build_bin_humbletest
    ):
    print " failed to load required Tools."
    exit(1)

# print out a few configuration options...
def Print_Env(env):
     print "Operating System: " + env['OS'] 
     print "\nC options:"
     print "\tenv['CC'] = '"+env['CC']+"'"
     print "\tenv['CFLAGS'] = '"+env['CFLAGS']+"'"
     print "\tenv['CCFLAGS'] = '",env['CCFLAGS']
     print "\tenv['CPPDEFSUFFIX'] = '"+env['CPPDEFSUFFIX']+"'"
     print "\tenv['CPPDEFINES'] = '",env['CPPDEFINES']
     print "\nC++ options: "
     print "\tenv['CXX'] = '"+ env['CXX'] +"'"
     print "\tenv['CXX'] = '"+ env['CXXFLAGS'] +"'"
     print "\nLinker Options: "
     print "\tenv['LINK'] = '"+ env['LINK'] +"'"	 
     print "\tenv['LINKFLAGS'] = '"+ env['LINKFLAGS'] +"'"	 
     print "\nPrefixes and Suffixes:"
     print "\tenv['LIBPREFIX'] = '"+env['LIBPREFIX']+"'"
     print "\tenv['LIBSUFFIX'] = '"+env['LIBSUFFIX']+"'"
     print "\tenv['OBJPREFIX'] = '"+env['OBJPREFIX']+"'"     
     print "\tenv['OBJSUFFIX'] = '"+env['OBJSUFFIX']+"'"
     print "\tenv['PROGPREFIX'] = '"+env['PROGPREFIX']+"'"          
     print "\tenv['PROGSUFFIX'] = '"+env['PROGSUFFIX']+"'"
     print "\npaths:"
     print "\tenv['LIBPATH'] = "
     print env['LIBPATH']
		
     print "\tenv['CPPPATH'] = "
     print env['CPPPATH'] 
env = Environment(OS=platform.system(),CPPDEFINES=[],CCFLAGS=[],TestFail=0, chdir='../..')
#append Global Build Params to help with determining which source files to include.
env.Append(
	OPENAL=0,OPENGL=0,
	SDL=0,SDLIMAGE=0, QTGUI=0,QTOPENGL=0,
	DX9 = 0, DX10 = 0, DX11 = 0);

print "Current System: ", env['OS'] 
if env['OS'] in ("Darwin", "Linux","cygwin"):
    env.Append(MACHINE = platform.machine())
    env.Append(OSVER   = platform.release())
    env.Append(MY_MACHINE = platform.machine())	
    env['build_platform'] = env['PLATFORM']
    env['target_platform'] = env['PLATFORM']

    print "Building on: ", env['MACHINE']
    env['wxconfig'] = 'wx-config'
    # Assume GCC, but compiler specific flags go here.
    if ARGUMENTS.get('debug', 0):
        env.Append(CCFLAGS = '-g')
    env.Append(CCFLAGS = ['-O3', '-Wall', '-Wno-parentheses','-Wno-sequence-point'])
    check_prereqs.Check_Prerequisites(env)
    alt_env = env.Clone()	
elif env['OS'] in ("Win32","Win64", "Windows"):
	# Assume Windows includes OpenGL, LibSDL and SDLimage.
	env['OPENGL'] = 1
	env['SDL'] = 1
	env['SDLIMAGE']=1
	env.Append(LIBS=['User32', 'Gdi32'])
	#if env['LINK'] in ( 'link'):
	#	env.Append(LINKFLAGS=['/machine:X86'])
	if(env['CC'] == 'gcc'):
		env['DX9'] = 1;
	if(env['CC'] == 'cl'):
		# Microsoft's Visual Studio C Compiler.
		WINSDK_DIR = os.getenv('WINSDK_DIR')		
		DXSDK_DIR = os.getenv('DXSDK_DIR')	
		if( WINSDK_DIR  is None):
			env['OPENGL'] = 0;
			print "Windows SDK Directory is not set."	
			print "Disabling OpenGL Rendering."
		else:
			print "Windows SDK Directory: \"" + WINSDK_DIR +"\""
			env.Append(CPPPATH=["\""+WINSDK_DIR + "\\include\""])
			env.Append(LIBPATH=["\""+WINSDK_DIR + "\\libs\""])
			env.Append(LIBS=['opengl32'])
		if( DXSDK_DIR  is None):
			print "Direct X SDK Directory is not set"	
			env.Append(DX9 = 1, DX10 = 1, DX11 = 1);
		else:
			print "Direct X SDK Directory: \"" + DXSDK_DIR +"\""
	alt_env = env.Clone()
	#Print_Env(env)
	#exit(1);
else:
	print "Unsupported Operating System: \"" + env['OS'] +"\""
	exit(1)
#Print_Env(env)
#exit();
if(env['OPENAL']):
    # this should run if OpenAL is found
    print "OpenAL Found."
if(env['TestFail']):
    #this should never run
    print "TestFail Found, error with build"
    exit(1)
env.Append(CPPPATH=Dir("src/includes"))
alt_env.Append(CPPPATH=Dir("../../src/includes"))





# Build Shared Libraries.

build_lib_HumbleCore.Build_lib_HumbleCore(alt_env)
if ( env['OPENGL'] == 1):
	# OpenGL Found.
	build_lib_HumbleGL.Build_lib_HumbleGL(alt_env)
if ( env['DX9'] == 1):
	# DirectX 9 Found
	#build_lib_HumbleGL.Build_lib_HumbleGL(alt_env)
	print "Building dx9"
if ( env['DX10'] == 1):
	# DirectX 10 Found
	#build_lib_HumbleGL.Build_lib_HumbleGL(alt_env)	
	print "Building dx10"
if ( env['DX11'] == 1):
	# DirectX 11 Found, and requires DX10.
	#build_lib_HumbleGL.Build_lib_HumbleGL(alt_env)		
	print "Building dx11"	
# build Static libs, and add to environment.
StaticLibs = []
StaticLibs += build_lib_TinyXML.BuildTinyXML(alt_env)
StaticLibs += build_lib_PAWN.BuildLibPAWN(alt_env)

alt_env.Append(LIBS = StaticLibs )

StaticLibsB = []
StaticLibsB += build_lib_SMALL.BuildLibSMALL(alt_env)

#build binaries.
build_bin_humbletest.Build2(alt_env, '')

